package com.example.test_1;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class demoDuoXuan extends AppCompatActivity {

    private String[] duoXuan = new String[]{"唱歌","篮球","足球","羽毛球","跑步"};

    private boolean[] bbb = new boolean[]{false,false,false,false,false};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_duo_xuan);

    }

    public void Duo(View view) {
        new AlertDialog.Builder(this)
                .setTitle("喜欢什么爱好")
                .setMultiChoiceItems(duoXuan, bbb, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int index, boolean isChecked) {
                        bbb[index] = isChecked;
                    }
                })
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int id) {
                        StringBuffer buffer = new StringBuffer();
                        for (int i1 = 0; i1 < bbb.length; i1++) {
                            if (bbb[i1]){
                                buffer.append(duoXuan[i1]).append("");
                            }
                        }
                        Toast.makeText(demoDuoXuan.this,buffer.toString(), Toast.LENGTH_SHORT).show();
                    }
                })
                .create().show();
    }
}